$(function() {

	oDiv1 = document.getElementById('div1');
	oDiv2 = document.getElementById('div2');

	drag(oDiv1);
	drag(oDiv2);

});

var oDiv1 = null;
var oDiv2 = null;

function drag(obj) {
	obj.onmousedown = function(ev) {
		ev = ev || event;
		// ie: 捕获拖文本默认事件
		if(this.setCapture) {
			this.setCapture();
		}

		var disX = ev.clientX - this.offsetLeft;
		var disY = ev.clientY - this.offsetTop;

		document.onmousemove = function(ev) {
			ev = ev || event;
			var L = ev.clientX - disX;
			var T = ev.clientY - disY;
			// 限制范围, 磁性吸附
			var clientWidth = document.documentElement.clientWidth || 
				document.body.clientWidth;
			var clientHeight = document.documentElement.clientHeight || 
				document.body.clientHeight;
			var lMin = 50;
			var lMax = clientWidth - obj.offsetWidth - 50;
			var tMin = 50;
			var tMax = clientHeight - obj.offsetHeight - 50;
			if(L < lMin) {
				L = 0;
			}
			if(L > lMax) {
				L = lMax + 50;
			}
			if(T < tMin) {
				T = 0;
			}
			if(T > tMax) {
				T = tMax + 50;
			}

			obj.style.left = L + 'px';
			obj.style.top = T + 'px';

		};

		document.onmouseup = function() {
			document.onmousemove = document.onmouseup = null;
			if(obj.releaseCapture) {
				obj.releaseCapture();
			}
		};
		// 去除默认行为, 拖文本
		return false;
	};
}
